#include "stm32f10x.h"                  // Device header
#include "L298N.h"
#include "Delay.h"
#include "OLED.h"
#include "RS485.h"
#include "Timer.h"
#include "PWM.h"
#include "DS18B20.h"

// 拉压力变送器读参数命令
uint8_t Control_frames_1[8] = {0x01, 0x03, 0x9c, 0x40, 0x00, 0x02, 0xEB, 0x8F};
uint8_t Control_frames_2[8] = {0x02, 0x03, 0x9c, 0x40, 0x00, 0x02, 0xEB, 0xBC};
volatile uint8_t state = 0;

u8 temp_int; // 用于存储温度的整数部分
float temp_data; // 用于存储完整的温度值（浮点数）



int main(void)
{
	/*模块初始化*/
	L298N_Init();				// L298N初始化
	Serial_Init();			// 串口初始化
	Timer_Init();				// 定时中断初始化
	OLED_Init();				// OLED初始化
	PWM_Init();					// PWM初始化 PA6管脚
	DS18B20_Init();     // 初始化温度传感器
	
	while (1)
	{	
		temp_int = (u8)temp_data;
		OLED_ShowSignedNum(1,5,temp_int,3);
    //一号传感器数据显示
		OLED_ShowString(1, 1, "NO1:");
		OLED_ShowHexNum(2, 1, Device1_RxPacket[3], 2);
		OLED_ShowHexNum(2, 4, Device1_RxPacket[4], 2);
		OLED_ShowHexNum(2, 7, Device1_RxPacket[5], 2);
		OLED_ShowHexNum(2, 10, Device1_RxPacket[6], 2);
		//二号传感器数据显示
		OLED_ShowString(3, 1, "NO2:");
		OLED_ShowHexNum(4, 1, Device2_RxPacket[3], 2);
		OLED_ShowHexNum(4, 4, Device2_RxPacket[4], 2);
		OLED_ShowHexNum(4, 7, Device2_RxPacket[5], 2);
		OLED_ShowHexNum(4, 10, Device2_RxPacket[6], 2);
		
		PWM_SetCompare1(100);
		Mx_A();
		
	}
		

} 

void TIM2_IRQHandler(void)
{
	temp_data = DS18B20_Get_Temp();
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
		{
			switch (state)
			{
				case 0:
						RS485_SendArray(USART1, Control_frames_1, 8);
						state = 1;  // 切换到下一个状态
						break;
				case 1:
						RS485_SendArray(USART1, Control_frames_2, 8);
						state = 0;  // 重置状态
						break;
			}
			TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
		}
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
	}
}




